﻿using System;
using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Driver;
using System.Data;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using SGC.BE;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using SGC.DL.InformeAuditoria.Mapeo;
using SGC.DL.PlanAuditoria.Mapeo;
using SGC.DL.ProcesoArea.Mapeo;
using SGC.DL.Proceso.Mapeo;
using SGC.DL.Area.Mapeo;
using SGC.DL.Reclamo.Mapeo;
using SGC.DL.OportunidadMejora.Mapeo;
using SGC.DL.SolicitudCambio.Mapeo;
using SGC.DL.Procedimiento.Mapeo;
using SGC.DL.Empleado.Mapeo;

namespace SGC.DL.Comun
{
    internal static class DL_NHibernate
    {
        private static Configuration ConfigureHibernate()
        {
            var configure = new Configuration();
            configure.SessionFactoryName("BuildIt");
            configure.DataBaseIntegration(db =>
            {
                db.Dialect<MsSql2012Dialect>();
                db.Driver<SqlClientDriver>();
                db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
                db.IsolationLevel = IsolationLevel.ReadCommitted;
                db.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Cn_Sql"].ToString();
                db.Timeout = 10;
                //debugging
                db.LogFormattedSql = true;
                db.LogSqlInConsole = true;
                db.AutoCommentSql = true;

            });
            return configure;
        }

        private static HbmMapping GetMappings()
        {
            ModelMapper mapper = new ModelMapper();
            mapper.AddMapping<DL_AreaMapeo>();
            mapper.AddMapping<DL_EmpleadoMapeo>();
            mapper.AddMapping<DL_ProcedimientoMapeo>();
            mapper.AddMapping<DL_ProcesoMapeo>();
            //mapper.AddMapping<DL_ProcesoAreaMapeo>();
            mapper.AddMapping<DL_PlanAuditoriaMapeo>();
            mapper.AddMapping<DL_PlanAuditoriaAuditorMapeo>();
            mapper.AddMapping<DL_InformeAuditoriaMapeo>();
            mapper.AddMapping<DL_ReclamoMapeo>();
            mapper.AddMapping<DL_OportunidadMejoraMapeo>();
            mapper.AddMapping<DL_SolicitudCambioMapeo>();
            HbmMapping mapping = mapper.CompileMappingFor(new[] 
            {
                typeof(BE_Area),
                typeof(BE_Empleado),
                typeof(BE_Procedimiento),
                typeof(BE_Proceso),                
                //typeof(BE_ProcesoArea),
                typeof(BE_PlanAuditoria),
                typeof(BE_PlanAuditor),
                typeof(BE_InformeAuditoria),                
                typeof(BE_Reclamo),
                typeof(BE_OportunidadMejora),
                typeof(BE_SolicitudCambio)
            });
            return mapping;
        }

        internal static ISessionFactory GetSession()
        {
            Configuration hbmConfig;
            ISessionFactory currentSession;
            hbmConfig = ConfigureHibernate();
            HbmMapping mappings = GetMappings();
            hbmConfig.AddDeserializedMapping(mappings, "SGC.DL");
            SchemaMetadataUpdater.QuoteTableAndColumns(hbmConfig);
            currentSession = hbmConfig.BuildSessionFactory();
            return currentSession;
        }

    }
}
